/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.aco.party.dao;

import org.apache.shiro.crypto.hash.Sha256Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * Loads sample data for the sample app since it's an in-memory database.
 */
@Component
public class BootstrapDataPopulator implements InitializingBean {
    @Resource
    private DataSource dataSource;

    /**
     * 进行内存数据库初始化！
     * 如果不是内存数据库的话，建议在初始化完数据库后，注释掉本方法体
     */
    public void afterPropertiesSet() throws Exception {

//        JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
//        Logger logger = LoggerFactory.getLogger(BootstrapDataPopulator.class.getName());
//        logger.info("插入数据库数据");
//        jdbcTemplate.execute("insert into wm_role values (1, 'user', 'The default role given to all users.')");
//        jdbcTemplate.execute("insert into wm_role values (2, 'vip', 'The default role given to all vips.')");
//        jdbcTemplate.execute("insert into wm_role values (3, 'admin', 'The administrator role only given to site admins')");
//
//        jdbcTemplate.execute("insert into wm_permission values (1, 'user:edit')");
//        jdbcTemplate.execute("insert into wm_permission values (2, 'vip:edit')");
//        jdbcTemplate.execute("insert into wm_permission values (3, 'vip:find')");
//        jdbcTemplate.execute("insert into wm_permission values (4, 'user:*')");
//        jdbcTemplate.execute("insert into wm_permission values (5, 'vip:*')");
//
//        jdbcTemplate.execute("insert into wm_role_permission values (1,1, 1)");
//        jdbcTemplate.execute("insert into wm_role_permission values (2,2, 2)");
//        jdbcTemplate.execute("insert into wm_role_permission values (3,2, 3)");
//        jdbcTemplate.execute("insert into wm_role_permission values (4,3, 4)");
//        jdbcTemplate.execute("insert into wm_role_permission values (5,3, 5)");
//
//        jdbcTemplate.execute("insert into wm_user(id,username,email,password) values (1, 'admin', 'sample@shiro.apache.org', '" + new Sha256Hash("admin").toHex() + "')");
//        jdbcTemplate.execute("insert into wm_user(id,username,email,password) values (2, 'user1', 'sample@shiro.apache.org', '" + new Sha256Hash("ddd").toHex() + "')");
//        jdbcTemplate.execute("insert into wm_user(id,username,email,password) values (3, 'vip1', 'sample@shiro.apache.org', '" + new Sha256Hash("ddd").toHex() + "')");
//        jdbcTemplate.execute("insert into wm_user(id,username,email,password) values (4, 'vip2', 'sample@shiro.apache.org', '" + new Sha256Hash("ddd").toHex() + "')");
//
//        jdbcTemplate.execute("insert into wm_user_role values (1,1, 3)");
//        jdbcTemplate.execute("insert into wm_user_role values (2,2, 1)");
//        jdbcTemplate.execute("insert into wm_user_role values (3,3, 2)");
//        jdbcTemplate.execute("insert into wm_user_role values (4,4, 2)");
//
//
//        jdbcTemplate.execute("commit");


    }
}
